package de.uhlesoluschns.opensearch.provider;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.widget.SimpleCursorAdapter;
import de.uhlesoluschns.opensearch.OpenSearch.Suggestion;

public class SuggestionsAdapter extends SimpleCursorAdapter {

	private Uri suggestionUri;
	private ContentResolver contentResolver;
	
	public SuggestionsAdapter(Context context, int layout, Cursor c,
			String[] from, int[] to, int engineId) {
		super(context, layout, c, from, to);
		contentResolver = context.getContentResolver();
		suggestionUri = Uri.withAppendedPath(Suggestion.CONTENT_URI, "" + engineId);
	}
	
	@Override
	public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
		System.out.println("SuggestionsAdapter.runQueryOnBackgroundThread() :: " + constraint);
		return contentResolver.query(suggestionUri, SuggestionsCursor.COLUMNS, constraint != null ? constraint.toString() : "", null, null);
	}
	
	@Override
	public CharSequence convertToString(Cursor cursor) {
		if(cursor != null) {
			int column = cursor.getColumnIndex(Suggestion.COL_SUGGESTION);
			if(column >= 0) {
				return cursor.getString(column);
			}
		}
		return null;
	}

}
